// Decompiled on 周六 2月 22 19:31:49 CST 2025 with Zomboid Decompiler v0.1.3 using Vineflower.
package zombie.iso.sprite;

import gnu.trove.map.hash.TIntObjectHashMap;
import java.util.HashMap;
import zombie.core.Color;
import zombie.debug.DebugLog;
import zombie.iso.SpriteDetails.IsoFlagType;

public final class IsoSpriteManager {
    public static final IsoSpriteManager instance = new IsoSpriteManager();
    public final HashMap<String, IsoSprite> NamedMap = new HashMap();
    public final TIntObjectHashMap<IsoSprite> IntMap = new TIntObjectHashMap<>();
    private final IsoSprite emptySprite = new IsoSprite(this);

    public IsoSpriteManager() {
        IsoSprite isoSprite = this.emptySprite;
        isoSprite.name = "";
        isoSprite.ID = -1;
        isoSprite.Properties.Set(IsoFlagType.invisible);
        isoSprite.CurrentAnim = new IsoAnim();
        isoSprite.CurrentAnim.ID = isoSprite.AnimStack.size();
        isoSprite.AnimStack.add(isoSprite.CurrentAnim);
        isoSprite.AnimMap.put("default", isoSprite.CurrentAnim);
        this.NamedMap.put(isoSprite.name, isoSprite);
    }

    public void Dispose() {
        IsoSprite.DisposeAll();
        IsoAnim.DisposeAll();
        Object[] object = this.IntMap.values();

        for (int _int = 0; _int < object.length; _int++) {
            IsoSprite isoSprite = (IsoSprite)object[_int];
            isoSprite.Dispose();
            isoSprite.def = null;
            isoSprite.parentManager = null;
        }

        this.IntMap.clear();
        this.NamedMap.clear();
        this.NamedMap.put(this.emptySprite.name, this.emptySprite);
    }

    public IsoSprite getSprite(int gid) {
        return this.IntMap.containsKey(gid) ? this.IntMap.get(gid) : null;
    }

    public IsoSprite getSprite(String gid) {
        return this.NamedMap.containsKey(gid) ? (IsoSprite)this.NamedMap.get(gid) : this.AddSprite(gid);
    }

    public IsoSprite getOrAddSpriteCache(String tex) {
        if (this.NamedMap.containsKey(tex)) {
            return (IsoSprite)this.NamedMap.get(tex);
        } else {
            IsoSprite isoSprite = new IsoSprite(this);
            isoSprite.LoadFramesNoDirPageSimple(tex);
            this.NamedMap.put(tex, isoSprite);
            return isoSprite;
        }
    }

    public IsoSprite getOrAddSpriteCache(String tex, Color col) {
        int int0 = (int)(col.r * 255.0F);
        int int1 = (int)(col.g * 255.0F);
        int int2 = (int)(col.b * 255.0F);
        String string = tex + "_" + int0 + "_" + int1 + "_" + int2;
        if (this.NamedMap.containsKey(string)) {
            return (IsoSprite)this.NamedMap.get(string);
        } else {
            IsoSprite isoSprite = new IsoSprite(this);
            isoSprite.LoadFramesNoDirPageSimple(tex);
            this.NamedMap.put(string, isoSprite);
            return isoSprite;
        }
    }

    public IsoSprite AddSprite(String tex) {
        IsoSprite isoSprite = new IsoSprite(this);
        isoSprite.LoadFramesNoDirPageSimple(tex);
        this.NamedMap.put(tex, isoSprite);
        return isoSprite;
    }

    public IsoSprite AddSprite(String tex, int ID) {
        IsoSprite isoSprite = new IsoSprite(this);
        isoSprite.LoadFramesNoDirPageSimple(tex);
        if (this.NamedMap.containsKey(tex)) {
            DebugLog.log("duplicate texture " + tex + " ignore ID=" + ID + ", use ID=" + ((IsoSprite)this.NamedMap.get(tex)).ID);
            ID = ((IsoSprite)this.NamedMap.get(tex)).ID;
        }

        this.NamedMap.put(tex, isoSprite);
        isoSprite.ID = ID;
        this.IntMap.put(ID, isoSprite);
        return isoSprite;
    }
}
